package com.jeff.cloud.provideruser.controller;


import com.jeff.cloud.provideruser.dao.UserRepository;
import com.jeff.cloud.provideruser.dao.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import sun.plugin.liveconnect.SecurityContextHelper;

import java.util.Collection;

/**
 * 打印当前登录的用户信息
 * @author: jeff
 * @date: 2021/8/6 11:11
 */
@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    // getMapping == @RequestMapping(method=RequestMethod.GET)
    @GetMapping("/{id}")
    public User findById(@PathVariable Long id){
        System.out.println("by jeff findby id="+id);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails){
            UserDetails user = (UserDetails) principal;
            Collection<? extends GrantedAuthority> collection = user.getAuthorities();
            for (GrantedAuthority c:collection){
                //打印当前登录用户的信息
                UserController.LOGGER.info("当前用户是{}，角色是{}",user.getUsername(),c.getAuthority());
            }
        }else{
            //do other things
        }
        User findOne = this.userRepository.findOne(id);
        return findOne;
    }


}
